/**
* @depends jquery.ui.js
*/

(function($) {

	$.contact = {
	
		add: function( user_id, callback ) {
		
			$.wajax( 'Contact_AddDialog', { id: user_id }, function( json ) {
				if( json.status != 'success' ) {
					return false;
				}
				var json = json.content;
				var dialog = $.window.create()
					.dialog( {
						title: 'Add contact',
						icon: '../../' + json.icon,
						buttons: {
							yes: function() {
								dialog.dialog( 'close' );
								$.wajax( 'Contact_Add', {
									id: user_id,
									message: textarea.hasClass( 'ui-default' ) ? undefined : textarea.attr( 'value' )
								}, function( json ) {
									if( json.status != 'success' ) {
										return false;
									}
									callback();
								} );
							},
							cancel: function() {
								dialog.dialog( 'close' );
							}
						}
					} );
				
				dialog.addClass( 'AddContactWindow' );
				dialog.dialog( 'content', json.content );
				var textarea = dialog.find( 'textarea' ).textarea( {
					defaultText: 'Send a message (optional)',
					defaultHeight: 50,
					minHeight: 50
				} );
				dialog.dialog( 'open' );
			} );
		},
		
		answer: function( request_id, callback ) {
			$.wajax( 'Contact_AnswerDialog', { id: request_id }, function( json ) {
				if( json.status != 'success' ) {
					return false;
				}
				var json = json.content;
				var dialog = $.window.create()
					.dialog( {
						title: 'Answer request',
						icon: '../../' + json.icon,
						buttons: {
							yes: function() {
								dialog.dialog( 'close' );
								$.wajax( 'Contact_Answer', {
									id: request_id,
									answer: 'yes'
								}, function( json ) {
									if( json.status != 'success' ) {
										return false;
									}
									callback();
								} );
							},
							no: function() {
								dialog.dialog( 'close' );
								$.wajax( 'Contact_Answer', {
									id: request_id,
									answer: 'no'
								}, function( json ) {
									if( json.status != 'success' ) {
										return false;
									}
									callback();
								} );
							},
							cancel: function() {
								dialog.dialog( 'close' );
							}
						}
					} );
			
				dialog.addClass( 'AnswerContactWindow' );
				dialog.dialog( 'content', json.content );
				dialog.dialog( 'open' );
				
			} );
		},
		
		remove: function( user_id, callback ) {
		
			$.wajax( 'Contact_RemoveDialog', { id: user_id }, function( json ) {
				if( json.status != 'success' ) {
					return false;
				}
				var json = json.content;
				var dialog = $.window.create()
					.dialog( {
						title: 'Confirmation',
						icon: '../../' + json.icon,
						buttons: {
							yes: function() {
								dialog.dialog( 'close' );
								$.wajax( 'Contact_Remove', {
									id: user_id
								}, function( json ) {
									if( json.status != 'success' ) {
										return false;
									}
									callback();
								} );
							},
							cancel: function() {
								dialog.dialog( 'close' );
							}
						}
					} )
					.addClass( 'RemoveContactWindow' )
					.dialog( 'content', json.content )
					.dialog( 'open' );
				
			} );
		},
		
		all: function( user_id ) {
			$.wajax( 'Contact_AllDialog', { id: user_id }, function( json ) {
				if( json.status != 'success' ) {
					return false;
				}
				var json = json.content;
				var window = $.window.create()
					.window( {
						title: 'Contacts of ' + json.fullname
						
					} );
				window.window( 'content', json.content );
				window.addClass( 'AllContactsWindow' );
				window.window( 'open' );
				
			} );
		}
	};
	
})(jQuery);
